<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Common\Model;
use Think\Model;
class NewtaskModel extends Model {
	/*自动验证*/
	protected $_validate = array (
		array('title','require','任务标题不能为空'),
		array('do_coin','require','任务奖励不能为空'),
	);

	protected $_auto = array (		
		array('dateline', NOW_TIME, self::MODEL_BOTH, 'string'),
		array('configs', 'formatconfigs', self::MODEL_BOTH, 'callback'),
	);

	/* 添加或更新数据 */
	public function update($data, $iscreate = TRUE) {		
		if ($iscreate == TRUE) $data = $this->create($data);
		if (empty($data)) {
			$this->error = $this->getError();
			return false;
		}
		if (isset($data['id']) && is_numeric($data['id'])) {
			$result = $this->save($data);
			if (!$result) {
				$this->error = '更新数据失败';
				return false;
			}
		} else {
			$result = $this->add($data);
			if ($result === false) {
				$this->error = '添加数据失败';
				return false;
			}
		}
		return $result;
	}

	public function formatconfigs($data) {
        foreach ($data as $key => $value) {
        	if(empty($value['task_id'])) continue;
            $configs[] = $value;
        }
        return array2string($configs);
	}
}
?>